/*
 * @Author: wjl 2820890765@qq.com
 * @Date: 2025-06-24 16:24:14
 * @LastEditors: wjl 2820890765@qq.com
 * @LastEditTime: 2025-06-25 10:38:11
 * @FilePath: /wzFront/src/permission.js
 * @Description:
 * Copyright (c) 2025 by wjl, All Rights Reserved.
 */
import router from "./router";
import { useAllDataStore } from "@/stores";
import { Storage, isEmpty } from "@/utils/common.js";
import { all } from "axios";

const whiteList = ["/login", "/information"]; // no redirect whitelist

const isRefresh = false;

router.beforeEach(async (to, from, next) => {
  const token = Storage.get("token");
  const allDataStore = useAllDataStore();
  if (whiteList.includes(to.path)) {
    next();
    return true;
  }
  if (isEmpty(token)) {
    next({
      path: "/login",
      query: { redirect: to.fullPath },
    });
    return false;
  }

  if (!allDataStore.state.isRefresh) {
    const routerList = allDataStore.state.routerList;
    const routes = await allDataStore.updateMenuList(routerList);
    await allDataStore.setRoute(routes);
    next({ ...to, replace: true });
  } else {
    next();
  }
});
